package SpLib.Tools
{
	import SpLib.Com.Com;
	import SpLib.std.map;
	import SpLib.std.iterator;
	
	import com.adobe.serialization.json.JSON;
	
	public class TDF
	{
		private var m_vFieldsType:Array;
		private var m_vFieldsName:Array;
		private var m_vRecords:SpLib.std.map;
		
		public function TDF()
		{
			m_vRecords = new SpLib.std.map();
		}
		
		private function ParseFields(line:String):Boolean
		{
			var fields:Array = line.split(",");
			if(fields==null || fields.length==0)
			{
				return false;
			}
			
			m_vFieldsType = new Array;
			
			for each(var field:String in fields)
			{
				switch(field)
				{
				case "d":
				case "s":
				case "f":
					m_vFieldsType.push(field);
				break;
				default:
				{
					m_vFieldsType = null;
					return false;
				}
				break;
				}
			}
			
			return true;
		}
		
		private function ParseFieldsName(line:String):Boolean
		{
			m_vFieldsName = line.split(",");
			if(m_vFieldsName==null || m_vFieldsName.length==0)
			{
				return false;
			}
			
			if(m_vFieldsType.length!=m_vFieldsName.length)
			{
				return false;
			}
			
			return true;
		}
		
		private function ParseRecord(line:String):Boolean
		{
			if(line.charAt(0)=="/" && line.charAt(1)=="/")
			{
				return true;
			}
			
			var fields:Array = line.split(",");
			if(fields==null || fields.length==0 || fields.length!=m_vFieldsName.length)
			{
				return false;
			}
			
			var datastr:String = "[" + line + "]";
			var record:Array = JSON.decode(datastr);
			var key:String = record[0].toString();
			
			if(m_vRecords.find(key)!=null)
			{
				SpLib.Tools.Tr.Error("重复主键行" + line);
				return false;
			}
			
			m_vRecords.insert(key,record);
			return true;
		}
		
		public function LoadFromBuf(data:String):Boolean
		{
			//检查头
			var left:Array = SpLib.Com.Com.PopLine(data);
			var line:String = left[0];
			data = left[1];
			if(line!="TDF1.0")
			{
				return false;
			}
			
			//读取字段类型
			left = SpLib.Com.Com.PopLine(data);
			line = left[0];
			data = left[1];
			if(!ParseFields(line))
			{
				return false;
			}
			
			//读取字段名
			left = SpLib.Com.Com.PopLine(data);
			line = left[0];
			data = left[1];
			if(!ParseFieldsName(line))
			{
				return false;
			}
			
			var ignore:Boolean =false;
			m_vRecords = new SpLib.std.map;
			var lineno:int = 0;
			while(true)
			{
				left = SpLib.Com.Com.PopLine(data);
				line = left[0];
				data = left[1];
			
				if(line==null || line.length==0)
				{
					if(data.length==0)
						break;
				}
				
				if(line=="*/")
				{
					ignore = false;
				}
				
				if(line=="/*")
				{
					ignore = true;
				}
				
				if(!ignore)
				{
					ParseRecord(line);
				}
			}
			
			return true;
		}
		
		public function ListKey():Array
		{
			var list:Array = new Array;
			var iter:SpLib.std.iterator = m_vRecords.begin();
			while(iter!=null)
			{
				list.push(iter.m_Key);
				iter = iter.next;
			}
			
			return list;
		}
		
		public function GetInt(key:String,name:String,def:int=0):int
		{
			var iter:SpLib.std.iterator = m_vRecords.find(key);
			if(iter==null)
			{
				return def;
			}
			
			var record:Array = iter.m_Data as Array;
			
			var pos:int = m_vFieldsName.indexOf(name);
			if(pos==-1)
			{
				return def;
			}
			
			return record[pos] as int;
		}
		
		public function GetStr(key:String,name:String,def:String = ""):String
		{
			var iter:SpLib.std.iterator = m_vRecords.find(key);
			if(iter==null)
			{
				return def;
			}
			
			var record:Array = iter.m_Data as Array;
			
			var pos:int = m_vFieldsName.indexOf(name);
			if(pos==-1)
			{
				return def;
			}
			
			return record[pos] as String;
		}
	}
}

